|
|
|
|
|
|
|
Microsoft Jet Transactions and the Workspace Object |
|
|
|
|
|
|
|
|
All Microsoft Jet transactions are applied to the current workspace object. (See Day 10, "Creating Database Programs with Visual Basic Code," for a discussion of the Workspace object.) If you do not name a Workspace object, Visual Basic uses the default workspace for your program. Because transactions apply to an entire workspace, it is recommended that you explicitly declare workspaces when you use transactions. This gives you the capability to isolate datasets into different workspaces and better control the creation of transactions. |
|
|
|
|
|
|
|
|
Here's the exact syntax for starting a transaction: |
|
|
|
|
|
|
|
|
Workspace(0).BeginTrans ' starts a transaction
...
If Err=0 Then
Workspaces(0).CommitTrans ' completes a transaction
Else
Workspaces(0).Rollback ' cancels a transaction
End If |
|
|
|
|
|
|
|
|
In this code, the default workspace for the transaction area is used. In an actual program, you should name a workspace explicitly. |
|
|
|
|
|
|
|
|
Building the Microsoft Jet Transaction Project |
|
|
|
|
|
|
|
|
You now build a small project that illustrates one possible use for transactions in your Visual Basic applications. You create a database routine that performs the tasks listed in the previous example. You open a transaction table, total the records to a subsidiary table, copy the records to a history file, and then delete the records from the original table. |
|
|
|
|
| |
|
|
|
|
To avoid errors when running this project, make sure that you selected the appropriate DAO reference before executing the program. Do this by choosing Project | References from the Visual Basic 5 menu. Then enable the checkbox next to Microsoft DAO 3.5 object library. |
|
|
|
|
|
|
|
|
|
|
Write two main routines: one to declare the workspace and open the database, and one to perform the database transaction. First, add the following code to the general declarations section of a new form in a new project: |
|
|
|
|
|
|
|
|
Option Explicit
Dim db As Database ' database object
Dim wsUpdate As workspace ' workspace object
Dim nErrFlag As Integer ' error flag |
|
|
|
|
|